 
import { request } from "../../request/index"
import { showModal,chooseAddress,showToast } from "../../utils/tools"
import regeneratorRuntime, { awrap } from '../../lib/runtime/runtime';

Page({ 
  /**
   * 页面的初始数据
   */
  data: {
    address:[],
    cart:[],  // 购物车数据 
    addNum:0,  // 总数
    addPrice:0,  // 总价
    allchecked:true
  },
 
  onShow(){
     let address =  wx.getStorageSync('address') || []
     // 获取本地缓存
     let cart = wx.getStorageSync('cart') || [] 

     address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
     this.setData({
       address
     })
    //  计算价格
     this.search(cart)
  },

  // 选择收货地址
  async handchooseAdress(){ 
    let res = await chooseAddress()
    wx.setStorageSync("address",res)
  },

  // 单击每个单选按钮
  handChange(e){ 
    let {goods_id} = e.currentTarget.dataset
    let cart = wx.getStorageSync('cart')
    // console.log(cart);
    const index = cart.findIndex(item => item.goods_id == goods_id) 
    cart[index].checked = !cart[index].checked
    wx.setStorageSync('cart', cart)
    this.search(cart)
  },

  // 计算价格 数量 封装
  search(cart){
    let allchecked = true
    // 总价格 总数量
    let addNum = 0  
    let addPrice = 0  
    cart.forEach(v => {
       if(v.checked){ 
         addPrice += v.goods_price * v.num
         addNum += v.num
       }else{  // 没选中
         allchecked = false
       }
    }); 
    // 防止 cart 为空
    allchecked = cart.length !=0 ? allchecked : false 
    this.setData({ 
      cart,
      allchecked,
      addNum,
      addPrice
    })   
  },
  
  // 全选
  handleItemAllCheck(){ 
    let {cart,allchecked} = this.data 
    allchecked = !allchecked
    if(allchecked){
      cart.forEach( (v,i)=>{
        v.checked = true
      })
      this.search(cart)
    }else{
      cart.forEach( (v,i)=>{
        v.checked = false
      })
      this.search(cart)
    }
   
    this.setData({
      cart,
      allchecked
    })
    wx.setStorageSync('cart', cart)
  },
 
  // 单击 加减
  async handclickBtn(e){
     let { goods_id,sub } = e.currentTarget.dataset
     let { cart,allchecked } = this.data
     // 查询单击产品的下标  
     let  index = cart.findIndex( v => v.goods_id == goods_id)  
     // 计算数量  
     cart[index].num += sub   

     let _this = this
     //  是否删除
     if( cart[index].num < 1 ){  
        cart[index].num = 1
        let res = await showModal({content:"确认要删除吗?"}) 
        if (res.confirm) {  // 单机确认
          cart[index].num = 0
          cart.splice(index,1)  
          _this.search(cart)
          if(cart.length == 0 ){
            allchecked = false
          }  
        } else if (res.cancel) {  // 取消
            cart[index].num = 1     
        }   
      }
       // 计算价格
       this.search(cart) 
       wx.setStorageSync('cart', cart)
       this.setData({
         cart,
         allchecked
       })
  },

  // 结算
  async handlePay(){
    let {address,cart,addPrice} = this.data  
    if(!address.userName){
       await showToast({ title:"你还没有选择收货地址",duration:1000,icon:"none"  }) 
       return
    }else if(cart.length == 0){
       await showToast({ title:"你还没有选购的商品",duration:1000,icon:"none"  }) 
       return
    }else{
      if( addPrice != 0 ){ // 总金额不为 0 可以 支付 
         wx.navigateTo({
           url: '/pages/pay/index',
         })
      }else{
        await showToast({ title:"请选择商品",duration:1000,icon:"none"  }) 
      }
    }
  }
})